/*
* created by Administrator on 2018/11/28
* 好的代码像粥一样,是得用时间和精力熬的。
*
* //ECMAscript 2015 [ES6]
* function log(n){
* return n;
* let log = n => n;
* 定义变量 【let const】
* let 代替 var 它能够形成（块级）作用域
*/
// const a = 0;
let a = 1;
let b = 2;
console.log(a+b);
// (function(){
// for(let i=0;i<10;i++){}
// })();
// console.log(i);  // 用let   结果是i is not defined

for(let i=0; i<10;i++){}
for(let i=0; i<11;i++){}
//const 定义的变量[常量]不能被重新赋值 但是可修改
const c = 11;
const o = {
    c: 11
};
o.c = 123;
console.log(o);


//字符串
//模板字符串
console.log('还有'+ b + "天就放假！");
console.log(`还有${b}天就放假！`);
let str = `还有${b}天就放假！`;
//for of 循环
for (let i of str){
    console.log(i);
}

//方法
function detect (str,char){
    return str.indexOf(char) > -1;
}
console.log(detect('hello','h'));
console.log('hello'.includes('h'));
console.log('a'.repeat(5));  //aaaaa

//解构赋值 [解析解构对应赋值]  --对应的赋值
let [a1,a2,a3,a4,a5,a6,a7,a8,a9] = str;
console.log(a1);
console.log(a2);
console.log(a8);
console.log(a9);   //undefined

// 数组 new Array
// 静态方法 Array.from 把类数组【ArrayLike】 转化成 数组
//扩展运算符 ...
//解构赋值


function toArray(arrayLike){
    return [].splice.call(arrayLike,0);
}

(function(){
    console.log(arguments);
    console.log(toArray(arguments));
    console.log(Array.from(arguments));
})(1,2,3,43,54,35,436,65);

let arr2 = [1,2,34,5,true,function() {}];
let arr3 = ['hello,world!',...arr2,'abc'];
console.log(arr3);

// let arr4 = arr2; //是不是复制?
let arr4 = [...arr2];
console.log(arr4);
arr2.push(123);
arr4.push("abc");
console.log(arr4);
console.log(arr2);

let json1 = JSON.stringify([1,2,3,4]);
let json2 = JSON.parse(JSON.stringify([1,2,3,4]));
console.log(json1);
console.log(json2);

function clone(obj) {
    return JSON.parse(JSON.stringify(obj));
}

let [g,h,j] = arr2;
console.log(g);  //1
console.log(h);  //2
console.log(j);  //34

//对象
//
var name1 = 'lipei';
// var age = '18';
//
// var o2 = {
//     name:name,
//     age:age,
//     coding: function(){
//         console.log('coding!!!');
//     }
// };
let x=1;
let y=2;
let name = 'lipei';
let age = '18';
let o3 = {x,y};

let o2 = {
    name,
    age,
    coding(){
        console.log('coding!!!');
    },
    ...o3,
};
console.log(o2);

let foo = {
    w:123,
    q:456,
};
let {q,w} = foo;
console.log(q);
console.log(w);


console.log('1234'.split(''));
console.log([...'1234']);
let numArr = [123,54354,32,45,65645,2344,456,345];
// console.log(Math.max([]));
console.log(Math.max.apply(null,[6,5,4,3,2,1]));
console.log(Math.max(...numArr));

//箭头函数
//this
function bar1(){
    let bar2 = () => {
        console.log(this);
    };
    console.log(this);
    bar2();
}
bar1.call(foo);
//箭头函数的三种写法
//如果函数体只有一行代码 默认return {}可省略
// let fn1 = () => {return 3};
let fn1 = () => 3;
// let fn2 = n => {return n};
let fn2 = n => n;
// let fn3 = (m,n) => {return m + n; };
let fn3 = (m,n) =>  m + n;

console.log(fn1());
console.log(fn2(10));
console.log(fn3(10, 10));

//求和
// function sum(){
//     var res = 0;
//     for(var i=0;i<arguments.length;i++){
//         res +=arguments[i];
//     }
//     return res;
// }

// let sum = (...arg) => {
//     let res =0;
//     arg.forEach((v) => res += v);
//     return res;
//     // console.log(res);
// };

// let sum = (...arg) => arg.reduce((previousValue,currentValue) => previousValue + currentValue);
// console.log(sum(1,3,4,5,6,7,8,9,0,4353,45));

function sum1(){
        return [].splice.call(arguments,0).reduce(function (previousValue,currentValue){
        return previousValue+currentValue;
    },0);
}
console.log(sum1(1, 3, 4, 5, 6, 7, 8, 9, 0, 4353, 45));

//函数的默认值
function add (m = 0,n = 0){
    return m + n;
}
console.log(add(10,20));
//es6
let add1 = (m=0,n=0) => m + n;
console.log(add1(12,12));




